<?php
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use App\Models\Memberorder;
use App\Models\Usersdomain;
use App\Models\Corplist;
use App\Services\OutputService;
use App\Services\MiniappService;
use App\Services\PayService;
use Exception,Log,Config,Pay;

class PriceController extends Controller
{
    public function products(Request $request)
    {
        $products = [
            "id"=>1,
            "platfrom_name"=>"微分组",
            "platfrom"=>"wegroup",
            "products"=>[
                [
                    "id"=>0,
                    "name"=>"免费版",
                    "label"=>"",
                    "active"=>2,
                    "prices"=>[
                        ["id"=>1,"name"=>"无需支付","active"=>2,"label"=>"","price"=>0.0,"price_original"=>"注册自动获取","day"=>7],
                    ],
                    "services"=>["创建单位数量 3个","单位有效期 3天","单位扫码上限 100次","单位数据统计分析 不支持","去广告 支持","专属人工客服 不支持"]
                ],
                [
                    "id"=>1,
                    "name"=>"商家版",
                    "label"=>"",
                    "active"=>1,
                    "prices"=>[
                        ["id"=>1,"name"=>"1月会员","active"=>2,"label"=>"","price"=>88,"price_original"=>"约3元每天","day"=>30],
                        ["id"=>2,"name"=>"1季会员","active"=>2,"label"=>"8折","price"=>211.2,"price_original"=>"约2.3元每天","day"=>90],
                        ["id"=>3,"name"=>"1年会员","active"=>1,"label"=>"6折","price"=>633.6,"price_original"=>"约1.7元每天","day"=>365],
                    ],
                    "services"=>["创建单位数量 无限制","单位有效期 会员到期时间","单位扫码上限 无限制","单位数据统计分析 支持","去广告 支持","专属人工客服 支持"]
                ]
            ],
            "paytype"=>["alipay","wechat"],
        ];
        return OutputService::success("SUCCESS",["products"=>$products]);
    }

    public function wechatMember(Request $request)
    {
        $openid = $request->openid;
        $out_trade_no = md5(time().rand("10000","99999"));
        $months = ["",1,3,12];
        $month = $months[$request->day_id];
        $products = ["",0.1,211.2,633.6];
        $price = $products[$request->day_id];
        $title = "会员(".$month."个月)";

        // 获取最新会员
        $memberOrder = Memberorder::userLastMember($openid);
        if (empty($memberOrder) || $memberOrder->end_time <= time()) {
            $end_time = strtotime("+".$month."months");
        } else {
            $end_time = strtotime("+".$month."months",$memberOrder->end_time);
        }
        // 添加新的订单
        Memberorder::addOrder($openid,$out_trade_no,$title,$end_time,$month,$price,1,$request->appid);
        if ($request->platefrom == "app") {
            $result = PayService::getPayParams("wechat","app",$title,$out_trade_no,$price,$openid,$request->appid,1);
        } else {
            $result = PayService::getPayParams("wechat","mini",$title,$out_trade_no,$price,$openid,$request->appid,1);
        }
        return OutputService::success("SUCCESS",["end_time"=>$end_time,"result"=>$result]);
    }

    public function wechatBack(Request $request)
    {
        $res = $request->getContent();
        $pay = Pay::wechat(config('pay.wechat'));
        $data = $pay->verify(); // 是的，验签就这么简单！
        if ($data->return_code != 'SUCCESS') return '支付失败';
        $order = Memberorder::getByOrderNo($data->out_trade_no);
        if (empty($order)) return '订单不存在';
        if ($order->pay_status != 1) return $pay->success();
        $total_fee = $data->total_fee/100;
        if ($order->price != $total_fee) return $pay->success();

        // 修改订单状态
        Memberorder::updatePayStatus($data->out_trade_no, [
            'pay_status' => 2,
            'transaction_id' => $data->transaction_id,
        ]);
        Corplist::corpListEndTime($order->openid,$order->end_time);
        return $pay->success();
    }

}